<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Forum Settings Controller
 *
 * @package		SimplyPost
 * @subpackage	Admin
 * @category	Controller
 * @author		Pascal Kriete
 */

class Settings extends Administrator {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Settings()
	{
		parent::Administrator();
		$this->lang->load('admin');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Show settings and handle changes
	 *
	 * @access	public
	 */
	function index()
	{
		if (!AJAX)
		{
			set_title($this->lang->line('board_settings_title'));
			$this->breadcrumb->from_path();
		}
		
		$this->load->helper('form');
		$this->load->library('validation');
		
		$rules['title'] = "trim|required|xss_clean";
		$this->validation->set_rules($rules);
		
		$fields['title'] = 'Name';
		$this->validation->set_fields($fields);
		
		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');
		
		$data['lock_status'] = BOARD_LOCKED ? 'Locked' : 'Unlocked';
		$data['lock_toggle'] = BOARD_LOCKED ? 'Unlock' : 'Lock';
		
		if ($this->validation->run() == FALSE)
		{
			if (!AJAX)
				$this->load->view('settings', $data);
			else
				die('Invalid Input');
		}
		else
		{
			$data = array('title' => $_POST['title']);
			$this->db->update('general', $data);
			
			if(!AJAX)
			{
				$this->session->set_flashdata('msg', $this->lang->line('settings_change_success'));
				redirect('admin/');
			}
			else
			{
				die($_POST['title']);
			}
		}		
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Lock/unlock board
	 *
	 * @access	public
	 */
	function toggle_lock()
	{		
		if (BOARD_LOCKED)
			$lock = 0;
		else
			$lock = 1;	

		$this->db->update('general', array('locked' => $lock), 'id = 1');
		$this->session->set_flashdata('msg', $this->lang->line('board_now_'.($lock ? 'locked' : 'unlocked')) );
		redirect('admin/settings');
	}

}